import { Command } from "commander";
import { Client, Wallet as XrplWallet } from "xrpl";
import { Log, LogStatus, LogType } from "../util/Logger";

const program = new Command("trust-line")
    .option("-w, --wallet-seed <value>", "Wallet seed")
    .option("-c, --currency-code <value>", "Issuer currency code")
    .option("-i, --issuer-address <value>", "Issuer address")
    .option("-n, --node <value>", "The network node", "wss://sidechain-net1.devnet.rippletest.net")
    .action(async ({ node, walletSeed, currencyCode, issuerAddress }) => {
        if (node && walletSeed && currencyCode && issuerAddress) {
            try {
                const client = new Client(node);
                await client.connect();

                const wallet = XrplWallet.fromSeed(walletSeed);

                Log(LogType.Tool, LogStatus.ToDo, `Creating TrustLine from ${wallet.address} to ${currencyCode}.${issuerAddress}...`);
                await client.submit(
                    {
                        TransactionType: "TrustSet",
                        Account: wallet.address,
                        LimitAmount: {
                            currency: currencyCode,
                            issuer: issuerAddress,
                            value: "100000000000000",
                        },
                    },
                    {
                        autofill: true,
                        wallet: wallet,
                    },
                );
                Log(LogType.Tool, LogStatus.Done, `TrustLine created from ${wallet.address} to ${currencyCode}.${issuerAddress}...`);
            } catch (error) {
                console.log(error);
                process.exit(1);
            }
            process.exit(0);
        } else {
            program.help();
            process.exit(1);
        }
    });
export default program;
